package cn.ainit.sys.query.mainList;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import cn.ainit.sys.common.dao.Pagination;
import cn.ainit.sys.common.dao.SysBaseDao;
import cn.ainit.sys.constant.SystemConstant;
import cn.ainit.sys.query.datastore.DataStore;
import cn.ainit.sys.query.entity.QueryButton;
import cn.ainit.sys.query.entity.QueryColumn;
import cn.ainit.sys.ui.buttons.FormButtons;
import cn.ainit.sys.ui.forms.Columns;

@Repository("mainListDaoImpl")
public class MainListDaoImpl extends SysBaseDao implements MainListDao{
	

	@Override
	public List<FormButtons> QueryUIButtons(String partOrAll, String id)
			throws Exception {
		String hql = "";
		if (partOrAll != null && partOrAll.equalsIgnoreCase(SystemConstant.QUERY_SECOND) ) {
			hql = " from FormButtons qb where qb.show = 'T' and qb.dsid=?";
		}else{
			hql = " from FormButtons qb where qb.show = 'T' and qb.dsid=?";
		}
		Query query = this.getSessionFactory().getCurrentSession().createQuery(hql);
		List<FormButtons> list = null;
		query.setString(0, id);
		if (query != null) {
			list = query.list();
		}
		return list;
	}

	@Override
	public List<QueryColumn> QueryShowColumns(String flag, String id)
			throws Exception {
		String hql = "";
		List<QueryColumn> listShow = null;
		try {
			hql = " from QueryColumn qc where qc.show = 'T' and qc.dsid=?";
			Query query = this.getSessionFactory().getCurrentSession().createQuery(hql);
			query.setString(0, id);
			listShow = query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listShow;
	}

	@Override
	public List<Columns> QueryUIColumns(String partOrAll, String formId)
			throws Exception {
		
		String hql = "";
		List<Columns> listShow = null;
		try {
			hql = " from Columns qc where qc.firstPageFlag = 'T' and qc.dsid =?";
			Query query = this.getSessionFactory().getCurrentSession().createQuery(hql);
			query.setString(0, formId);
			listShow = query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listShow;
	}

	@Override
	public DataStore querySql(String id) throws Exception {
		try {
			String hql = "from DataStore where dsId =?";
			Query query = sessionFactory.getCurrentSession().createQuery(hql);
			query.setString(0, id);
			List<Object> objects = query.list();
			DataStore dataStore = new DataStore();
			if (objects != null && objects.size() > 0 ) {
				dataStore =  (DataStore) objects.get(0);
			}
			return dataStore;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public List getRows(String sql, int pageNumber, int pageSize) throws Exception {
		List<Object> list = null;
		try {
			Pagination pagination = new Pagination(dbType, sql, pageNumber, pageSize, this.getJdbcTemplate());
			list = pagination.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<QueryButton> QueryButtons(String partOrAll, String id)
			throws Exception {
		String hql = "";
		if (partOrAll != null && partOrAll.equalsIgnoreCase(SystemConstant.QUERY_SECOND) ) {
			hql = " from QueryButton qb where qb.secondPageFlag = 'T' and qb.dsid=?";
		}else{
			hql = " from QueryButton qb where qb.firstPageFlag = 'T' and qb.dsid=?";
		}
		Query query = this.getSessionFactory().getCurrentSession().createQuery(hql);
		query.setString(0, id);
		List<QueryButton> list = null;
		list = query.list();
		return list;
	}

 
}
